﻿using Inspection.Helper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using Vision.AlgBase;

namespace Inspection.Models
{
    /// <summary>
    /// 配方对象
    /// </summary>
    [Serializable]
    public class ProjectRecipe : PropertyChangedNotify
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; OnPropertyChanged(); }
        }
        private string name = string.Empty;

        /// <summary>
        /// 算法集合
        /// </summary>
        public ObservableCollection<IAlg> ExcuteAlgList
        {
            get { return excuteAlgList; }
            set { excuteAlgList = value; OnPropertyChanged(); }
        }
        private ObservableCollection<IAlg> excuteAlgList = new ObservableCollection<IAlg>();


        /// <summary>
        /// 保存项目
        /// </summary>
        /// <param name="project"></param>
        /// <param name="filePath"></param>
        public void SavaProject()
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + FolderName;
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            filePath = Path.Combine(filePath, $"{name}{PrjExten}");
            FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            BinaryFormatter binary = new BinaryFormatter();
            binary.Serialize(fs, this);
            fs.Flush();
            fs.Close();
            fs.Dispose();
        }

        /// <summary>
        /// 加载项目
        /// </summary>
        /// <param name="recipeName"></param>
        /// <returns></returns>
        public static ProjectRecipe LoadProject(string recipeName)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + FolderName;
             filePath = Path.Combine(filePath, $"{recipeName}{PrjExten}");
            if (!File.Exists(filePath)) return null;
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryFormatter binary = new BinaryFormatter();
            ProjectRecipe recipe=(ProjectRecipe) binary.Deserialize(fs);
            fs.Close();
            fs.Dispose();
            return recipe;
        }

        public const string PrjExten = ".Project";
        public const string FolderName = "Recipe";
    }
}
